home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 176-200 / scopedisk192 / unzipv3.1 / unreduce.c < prev    next >
Text File  |  1995-03-19  |  5KB  |  154 lines

  1. /* ----------------------------------------------------------- */
  2.  
  3. void LoadFollowers()
  4. {
  5.     register int x;
  6.     register int i;
  7.  
  8.     for (x = 255; x >= 0; x--) {
  9.         READBIT(6,Slen[x]);
  10.         for (i = 0; i < Slen[x]; i++) {
  11.             READBIT(8,followers[x][i]);
  12.         }
  13.     }
  14. }
  15.  
  16.  
  17. /* ----------------------------------------------------------- */
  18. /*
  19.  * The Reducing algorithm is actually a combination of two
  20.  * distinct algorithms.  The first algorithm compresses repeated
  21.  * byte sequences, and the second algorithm takes the compressed
  22.  * stream from the first algorithm and applies a probabilistic
  23.  * compression method.
  24.  */
  25.  
  26. int L_table[] = {0, 0x7f, 0x3f, 0x1f, 0x0f};
  27.  
  28. int D_shift[] = {0, 0x07, 0x06, 0x05, 0x04};
  29. int D_mask[]  = {0, 0x01, 0x03, 0x07, 0x0f};
  30.  
  31. int B_table[] = {8, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5,
  32.                  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
  33.                  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  34.                  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7,
  35.                  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  36.                  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  37.                  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  38.                  7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  39.                  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  40.                  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  41.                  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  42.                  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  43.                  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  44.                  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  45.                  8, 8, 8, 8};
  46.  
  47. /* ----------------------------------------------------------- */
  48.  
  49. void unReduce()
  50.  /* expand probablisticly reduced data */
  51. {
  52.     register int lchar;
  53.     int nchar;
  54.     int ExState;
  55.     int V;
  56.     int Len;
  57.  
  58.     factor = lrec.compression_method - 1;
  59.     ExState = 0;
  60.     lchar = 0;
  61.     LoadFollowers();
  62.  
  63.     while (((outpos+outcnt) < ucsize) && (!zipeof)) {
  64.         if (Slen[lchar] == 0)
  65.             READBIT(8,nchar)      /* ; */
  66.         else
  67.         {
  68.             READBIT(1,nchar);
  69.             if (nchar != 0)
  70.                 READBIT(8,nchar)      /* ; */
  71.             else
  72.             {
  73.                 int follower;
  74.                 int bitsneeded = B_table[Slen[lchar]];
  75.                 READBIT(bitsneeded,follower);
  76.                 nchar = followers[lchar][follower];
  77.             }
  78.         }
  79.  
  80.         /* expand the resulting byte */
  81.         switch (ExState) {
  82.  
  83.         case 0:
  84.             if (nchar != DLE)
  85.                 OUTB(nchar) /*;*/
  86.             else
  87.                 ExState = 1;
  88.             break;
  89.  
  90.         case 1:
  91.             if (nchar != 0) {
  92.                 V = nchar;
  93.                 Len = V & L_table[factor];
  94.                 if (Len == L_table[factor])
  95.                     ExState = 2;
  96.                 else
  97.                     ExState = 3;
  98.             }
  99.             else {
  100.                 OUTB(DLE);
  101.                 ExState = 0;
  102.             }
  103.             break;
  104.  
  105.         case 2: {
  106.                 Len += nchar;
  107.                 ExState = 3;
  108.             }
  109.             break;
  110.  
  111.         case 3: {
  112.                 register int i = Len + 3;
  113.                 int offset = (((V >> D_shift[factor]) &
  114.                                      D_mask[factor]) << 8) + nchar + 1;
  115.                 longint op = (outpos+outcnt) - offset;
  116.  
  117.                 /* special case- before start of file */
  118.                 while ((op < 0L) && (i > 0)) {
  119.                     OUTB(0);
  120.                     op++;
  121.                     i--;
  122.                 }
  123.  
  124.                 /* normal copy of data from output buffer */
  125.                 {
  126.                     register int ix = (int) (op % OUTBUFSIZ);
  127.  
  128.                     /* do a block memory copy if possible */
  129.                     if ( ((ix    +i) < OUTBUFSIZ) &&
  130.                       ((outcnt+i) < OUTBUFSIZ) ) {
  131.                         zmemcpy(outptr,&outbuf[ix],i);
  132.                         outptr += i;
  133.                         outcnt += i;
  134.                     }
  135.  
  136.                     /* otherwise copy byte by byte */
  137.                     else while (i--) {
  138.                         OUTB(outbuf[ix]);
  139.                         if (++ix >= OUTBUFSIZ)
  140.                             ix = 0;
  141.                     }
  142.                 }
  143.  
  144.                 ExState = 0;
  145.             }
  146.             break;
  147.         }
  148.  
  149.         /* store character for next iteration */
  150.         lchar = nchar;
  151.     }
  152. }
  153.  
  154.